<!DOCTYPE html>
<html>
	<head>
		<script src="js/core-min.js"></script>
		<script src="js/hmac-min.js"></script>
		<script src="js/sha256-min.js"></script>
		<script src="js/pbkdf2-min.js"></script>
		<script src="js/jquery-min.js"></script>
		<script type="text/javascript">
			// orion elenzil
			// 20080905
			 
			function getValueOfDigit(digit, alphabet)
			{
			   var pos = alphabet.indexOf(digit);
			   return pos;
			}
			 
			function convert(src, srcAlphabet, dstAlphabet)
			{
			   var srcBase = srcAlphabet.length;
			   var dstBase = dstAlphabet.length;
			 
			   var wet     = src;
			   var val     = 0;
			   var mlt     = 1;
			 
			   while (wet.length > 0)
			   {
				 var digit  = wet.charAt(wet.length - 1);
				 val       += mlt * getValueOfDigit(digit, srcAlphabet);
				 wet        = wet.substring(0, wet.length - 1);
				 mlt       *= srcBase;
			   }
			 
			   wet          = val;
			   var ret      = "";
			 
			   while (wet >= dstBase)
			   {
				 var digitVal = wet % dstBase;
				 var digit    = dstAlphabet.charAt(digitVal);
				 ret          = digit + ret;
				 wet /= dstBase;
			   }
			 
			   var digit    = dstAlphabet.charAt(wet);
			   ret          = digit + ret;
			   
			   return ret;
			}
		</script>
		<script type="text/javascript">
			// Generate password.
			function getPass(master, salt) {
				var hash = CryptoJS.PBKDF2(master, salt, { keySize: 256/32, iterations: 1000, hasher: CryptoJS.algo.SHA256 }).toString();
				var fullpass = convert(hash,"0123456789abcdef","0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
				var pass = "";
				var chars = 0;
				for (i = 0; chars < 12; i++) {
					j = i % 12;
					if (chars == 0 && fullpass.substring(j,j+1).toUpperCase() != fullpass.substring(j,j+1)) {
						pass += fullpass.substring(j,j+1);
						chars++;
					} else if (chars == 1 && parseInt(fullpass.substring(j,j+1)) == fullpass.substring(j,j+1)) {
						pass += fullpass.substring(j,j+1);
						chars++;	
					} else if (chars == 2 && fullpass.substring(j,j+1).toLowerCase() != fullpass.substring(j,j+1)) {
						pass += fullpass.substring(j,j+1);
						chars++;
					} else if (chars > 2) {
						pass += fullpass.substring(j,j+1);
						chars++;						
					} else if (chars == 0 && i >= fullpass.length) {
						pass += "f";
						chars++
					} else if (chars == 1 && i >= fullpass.length*2) {
						pass += "7";
						chars++
					} else if (chars == 2 && i >= fullpass.length*3) {
						pass += "T";
						chars++
					}
				}
				return pass;
			}
			
			// Page functionality.
			function update() {
				$('#output').addClass('hide');
				if ($('#password').val().length < 3 || $('#salt').val().length < 1) {
					$('#output').text("Enter a master password and site key to generate your password.")
				} else {
					$('#output').text("Generating...")
					setTimeout(function () {
						$('#output').html("Password to use<br /><input type='text' value='"+getPass($('#password').val(),$('#salt').val())+"' /><br /><span>Click to view, long click to copy</span");
					}, 0);
				}
			}
			
			$(function () {
				$('#generate').click(update);
				update();
			});
		</script>
		<style>
			body {
				padding: 10px;
				font-family: sans-serif;
				font-size: 1.2em;
				text-align: center;
			}
			input {
				font-size: 1.3em;
				max-width: 90%;
			}
			#output {
				padding-top: 20px;
			}
			#output input {
				font-family: monospace;
			}
			.hide input {
				color: white;
			}
			.hide input:focus {
				color: black;
			}
			p {
				font-size: 0.8em;
			}
			#update {
				font-size: 0.7em;
				margin-top: 10px;
				border: 1px dashed #888;
				padding: 10px;
			}
		</style>
	</head>
	<body>
		Master password<br />
		<input type="password" id="password" value="" /><br />
		Site key (e.g. amazon.com)<br />
		<input type="text" id="salt" value="" />
		<button id="generate">Generate</button>
		<div id="output" class="hide"></div>
		<p>Use this app to generate unique and secure passwords for different purposes from a single master password.</p>
		<p>From a computer use the "PassHash" Chrome extension, or go to passhash.connorhd.co.uk to generate the same passwords from any browser.</p>
		<div id="update">Algorithm updated 2012-06-10, to generate old PassHash passwords <a target="_blank" href="http://passhash.connorhd.co.uk/v1">click here</a></div>
	</body>
</html>
